package ltd.hxya.novel.crawl.job;

import com.xxl.job.core.handler.annotation.XxlJob;
import lombok.extern.slf4j.Slf4j;
import ltd.hxya.novel.common.framework.CustomFuture;
import ltd.hxya.novel.common.to.book.BookTo;
import ltd.hxya.novel.crawl.bean.CrawlVo;
import ltd.hxya.novel.crawl.entity.UnexecutedTask;
import ltd.hxya.novel.crawl.feign.BookFeignService;
import ltd.hxya.novel.crawl.service.CrawlTaskService;
import ltd.hxya.novel.crawl.service.ICrawlSourceService;
import ltd.hxya.novel.crawl.service.IUnexecutedTaskService;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ThreadPoolExecutor;

@Slf4j
@Component
public class JobHandler {

    @Autowired
    private CrawlTaskService crawlTaskService;

    @Autowired
    private ICrawlSourceService crawlSourceService;

    @Autowired
    private BookFeignService bookFeignService;

    @Autowired
    private IUnexecutedTaskService unexecutedTaskService;

    @Autowired
    private RabbitTemplate rabbitTemplate;

    @Autowired
    private ThreadPoolExecutor threadPoolExecutor;


    @XxlJob("crawl_novel")
    public void  crawlNovel() throws IOException {
        //调用远程服务，查询小说id
        crawlTaskService.renewNovel();
    }



    @XxlJob("crawl_unexecuted_task")
    public void crawlExecutedTask() throws InterruptedException {
        //从数据库中查询出任务，并开始爬取任务
        List<UnexecutedTask> unexecutedTasks = unexecutedTaskService.getBaseMapper().selectList(null);
        unexecutedTasks.forEach(unexecutedTask -> {
            CustomFuture.runExec(()-> transformBookTo(unexecutedTask),threadPoolExecutor);
        });

    }


    private void transformBookTo(UnexecutedTask unexecutedTask) {
        BookTo bookTo = new BookTo();
        bookTo.setCrawlBookId(unexecutedTask.getCrawlBookId());
        bookTo.setBookName(unexecutedTask.getBookName());
        bookTo.setCrawlSourceId(unexecutedTask.getCrawlSourceId());
        crawlChapter(bookTo);
    }

    public void crawlChapter(BookTo bookTo) {
        crawlTaskService.crawlChapter(bookTo);

    }

    //爬取出所有有效的页面
    @XxlJob("crawl_all")
    public void crawlAllNovel() throws IOException {
        //从数据库中查询出所有的爬虫源
        List<CrawlVo> crawlVoList = crawlSourceService.crawlSourceList();
        for (CrawlVo crawlVo : crawlVoList) {
            //从数据库中查询到所有的信息
            //List<String> urlList = crawlTaskService.allAvailableUrl(crawlVo);
        }
        //爬取小说
    }

    @XxlJob("renew_novel")
    public void renewNovel(){
        //更新小说
        List<CrawlVo> crawlVoList = crawlSourceService.crawlSourceList();
        Map<String,List<String>> crawlUrlMap = new HashMap<>();

        for (CrawlVo crawlVo : crawlVoList) {
            CompletableFuture<Void> completableFuture = CompletableFuture.runAsync(() -> {
                try {
                    crawlTaskService.allAvailableUrl(crawlVo,crawlUrlMap);
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }, threadPoolExecutor);
            completableFuture.exceptionally(throwable -> {
                throwable.printStackTrace();
                return null;
            });
        }
    }
}
